package com.lq.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.lq.dao.UserDao;
import com.lq.domain.User;
import com.lq.service.UserService;
import com.lq.util.JsonUtil;
import com.lq.util.Md5Token;



/**
 * 类说明：用户Service实现
 * 
 * @author 作者: LiuJunGuang
 * @version 创建时间：2012-3-25 下午02:26:48
 */
@Service
@Transactional
public class UserServiceImpl implements UserService {

	@Autowired
	private UserDao userDao;

	// 添加用户
	public void addUser(User user) {
		if (!user.getUsername().isEmpty() && !user.getPassword().isEmpty() ) {
			user.setPassword(Md5Token.getInstance().getLongToken(user.getPassword().toString().trim()));
			userDao.insertUser(user);
		}
	}

	// 更新用户
	public void updateUser(User user) {
		userDao.updateUser(user);
	}

	public void deleteUser(Integer userId) {
		userDao.deleteUser(userId);
	}

	public User findUserById(Integer userId) {
		return userDao.findUserByid(userId);
	}

	public List<User> findAllUser() {
		return userDao.findAll();
	}

	public User login(User user) {
		user.setPassword(Md5Token.getInstance().getLongToken(user.getPassword()));
			return userDao.userLogin(user);
	}
	
	//APP登陆接口
	public String Login(User user,String appVersion) {
		
		String json =null;
		user = userDao.userLogin(user);
		if (user.getIsAble() == 1){
			json = JsonUtil.objectToJson(user);
		}else{
			json = "账号被锁定，请联系客服！";
		}
		
		return json;
		
	}
}
